package com.dxy.chatserver.mapper;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.SystemClock;
import com.dxy.chatserver.db.entity.AcceptLineMsgResponse;
import com.dxy.chatserver.config.MongoBuilder;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCursor;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;


@Component
@Slf4j
public class ChatMsgMapper{

    public final static String DB_NAME = "online_message";

    private final MongoTemplate mongoTemplate;

    public ChatMsgMapper(@Qualifier("chatMsgMongoClient") MongoClient mongoClient) {
        this.mongoTemplate = MongoBuilder.template(mongoClient, DB_NAME);
    }

    public List<AcceptLineMsgResponse> find(String session, Long startTime, Long endTime) {
        Query query = new Query();
        startTime = Objects.isNull(startTime) ? 0 : startTime;
        endTime = Objects.isNull(endTime) ? SystemClock.now() : endTime;
        query.addCriteria(Criteria.where("createTime").gt(startTime).lte(endTime));
        List<AcceptLineMsgResponse> acceptLineMsgResponses = mongoTemplate.find(query, AcceptLineMsgResponse.class, session);
        return acceptLineMsgResponses;
    }

    public MongoCursor<Document> findDBObject(String session, Long startTime, Long endTime) {
        BasicDBObject query = new BasicDBObject();
        startTime = Objects.isNull(startTime) ? 0 : startTime;
        endTime = Objects.isNull(endTime) ? SystemClock.now() : endTime;
        query.put("createTime", (new BasicDBObject("$gt",startTime)).append("$lte",endTime));
        return mongoTemplate.getCollection(session).find(query).batchSize(10000).cursor();
    }

    public List<AcceptLineMsgResponse> find(String session){
        List<AcceptLineMsgResponse> acceptLineMsgResponses = mongoTemplate.findAll(AcceptLineMsgResponse.class,session);
        return acceptLineMsgResponses;
    }

    public long count(String session){
        System.out.println(DateUtil.current());
        long count = mongoTemplate.count(new Query(), session);
        System.out.println(DateUtil.current());
        log.info("当前数量{}",count);
        return count;
    }

    public void getDbList(){
        AtomicReference<Long> total_count = new AtomicReference<>(0L);
        Set<String> collect = this.getDbNameList(null);
        collect.forEach(o->{
            long count = mongoTemplate.count(new Query(), o);
            total_count.set(count + total_count.get());
            log.info("当前表:{},共计数据:{}",o,count);

        });
        System.out.println("总共表 : "+collect.size()+"总计数据:"+total_count);
    }

    public void getCount(String session, Long startTime, Long endTime){
        Query query = new Query();
        startTime = Objects.isNull(startTime) ? 0 : startTime;
        endTime = Objects.isNull(endTime) ? SystemClock.now() : endTime;
        query.addCriteria(Criteria.where("createTime").gt(startTime).lte(endTime));
        long count = mongoTemplate.count(query, session);
        log.info("当前表大小,{}",count);
    }

    public TreeSet<String> getDbNameList(String filterName){
        Set<String> collectionNames = mongoTemplate.getCollectionNames();
        if(collectionNames.isEmpty()){
            return null;
        }
        if(StringUtils.hasText(filterName)){
            collectionNames =  collectionNames.stream().filter(o -> o.contains(filterName)).collect(Collectors.toSet());
        }
        TreeSet<String> strings = new TreeSet<>();
        strings.addAll(collectionNames);
        return strings;
    }
}
